withdrawalsController = BaseController.extend({
	template: 'withdrawals',
	center: '提现',
	mainView: 'withdrawals',
	waitOn: function () {
		let self = this;
		self.wallet_mobile = '';
		self.last_money = '';
		Tracker.autorun(function(){
			self.merchantInfo = self.currentUser();
			if(self.merchantInfo){
				return Meteor.subscribe('wallets',{merchant_id: parseInt(self.merchantInfo.id)},{});
			}
		})
	},
	onRendered: function (controller) {
		let self = this;
		var myApp = new Framework7({
			modalTitle: "提示",
			modalButtonOk: "确定",
			modalButtonCancel: "取消",
		});
		var $$ = Dom7;
		$$('.form-to-data').on('click', function () {
			var formData = myApp.formToData('#withdrawal');
			let m_code = $('#code').val();

			if(formData.money){
				if (formData.money < self.last_money) {
					if(formData.money && m_code){
						myApp.confirm('确定提现？', function () {
							var post_data = {
								merchant_id: self.merchantInfo.id,
								draw_fee: formData.money,
								code: m_code,
							};
							controller.post('merchant/draw', post_data).then(function (res) {
								if (res.data && ('OK' == res.data.state)) {
									myApp.alert('提现申请已发出，将在一个工作日到账');
								}else{
									myApp.alert('申请提现失败，请稍后再试');
								}
							}, function (errer) {
								myApp.alert('申请提现失败，请稍后再试');
							})
						});
					}else{
						myApp.alert('请输入验证码');
					}
				} else {
					myApp.alert('提现金额不得超过账户余额，请重新输入');
				}
			}else{
				myApp.alert('请输入提现金额');
			}
			
		});
	},
	data: function () {
		let self = this;
		return {
			wallet:function(){
				let walletOne = Wallets.findOne();
				self.wallet_mobile = walletOne?walletOne.mobile:'';
				self.last_money = walletOne?walletOne.last_money:0;
				return walletOne;
			},
		}
	},
	events: function (controller) {
		let self = this;
		return {
			'click #getPhone': function (e) {
				if(self.wallet_mobile){
					controller.get('user/mobile-code/' + self.wallet_mobile).then(function (res) {
						if (res.data.state == "OK") {
							$(e.currentTarget).attr('disabled','disabled');
							countDown();
						}
					}, function (error) {
						console.log(error)
					});
				}else{
					controller.app.alert('请先绑定手机号');
				}
	
				function countDown() {
					let index = 59;
					let timeout = setInterval(function(){
						$(e.currentTarget).html(index+'秒');
						if(--index){
			
						}else{
						  $(e.currentTarget).removeAttr('disabled');
						  $(e.currentTarget).html('获取验证码');
						  clearInterval(timeout);
						}
					},1000);
				}
			}
		};
	}
})